﻿using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;

static public class NavigationManager
{
#if !UNITY_EDITOR && UNITY_ANDROID
    [DllImport("Navigation", EntryPoint = "CreateNavigation")]
    private static extern void CreateNavigation();

    [DllImport("Navigation", EntryPoint = "ReleaseNavigation")]
    private static extern void ReleaseNavigation();

    [DllImport("Navigation", EntryPoint = "LoadTile")]
    private static extern void LoadTile(string filePath);

    [DllImport("Navigation", EntryPoint = "LoadTileFromBuffer")]
    private static extern void LoadTileFromBuffer(byte[] data,int dataSize);


    [DllImport("Navigation", EntryPoint = "FindRandomPointAroundCircle")]
    private static extern uint FindRandomPointAroundCircle( ref NavPoint navPoint,float radius,ref NavPoint navPointOut);

    [DllImport("Navigation", EntryPoint = "FindNearestPoly")]
    private static extern uint FindNearestPoly(float[] pos, ref NavPoint navPoint,  float[] polyPickExt);

    [DllImport("Navigation", EntryPoint = "FindNearestPolyNoPickExt")]
    private static extern uint FindNearestPolyNoPickExt(float[] pos,ref NavPoint navPoint);

    [DllImport("Navigation", EntryPoint = "FindPathStraight")]
    private static extern uint FindPathStraight(float[] startPos, float[] endPos,  float[] out_list,ref int count);

    [DllImport("Navigation", EntryPoint = "FinRaycast")]
    private static extern uint FinRaycast(float[] startPos, float[] endPos, float[] hitPos, float[] tangent);

    [DllImport("Navigation", EntryPoint = "DtStatusSucceed")]
    private static extern bool DtStatusSucceed(uint status);
#else
    [DllImport("libNavigation", EntryPoint = "CreateNavigation")]
    private static extern void CreateNavigation();

    [DllImport("libNavigation", EntryPoint = "ReleaseNavigation")]
    private static extern void ReleaseNavigation();

    [DllImport("libNavigation", EntryPoint = "LoadTile")]
    private static extern void LoadTile(string filePath);

    [DllImport("libNavigation", EntryPoint = "LoadTileFromBuffer")]
    private static extern void LoadTileFromBuffer(byte[] data,int dataSize);


    [DllImport("libNavigation", EntryPoint = "FindRandomPointAroundCircle")]
    private static extern uint FindRandomPointAroundCircle( ref NavPoint navPoint,float radius,ref NavPoint navPointOut);

    [DllImport("libNavigation", EntryPoint = "FindNearestPoly")]
    private static extern uint FindNearestPoly(float[] pos,ref NavPoint navPoint,float[] polyPickExt);

    [DllImport("libNavigation", EntryPoint = "FindNearestPolyNoPickExt")]
    private static extern uint FindNearestPolyNoPickExt(float[] pos, ref NavPoint navPoint);

    [DllImport("libNavigation", EntryPoint = "FindPathStraight")]
    private static extern uint FindPathStraight(float[] startPos, float[] endPos, float[] out_list,ref int count);

    [DllImport("libNavigation", EntryPoint = "FinRaycast")]
    private static extern uint FinRaycast(float[] startPos, float[] endPos,  float[] hitPos,  float[] tangent);

    [DllImport("libNavigation", EntryPoint = "DtStatusSucceed")]
    private static extern bool DtStatusSucceed(uint status);
#endif

    private static bool m_IsInitial = false;

    static NavigationManager()
    {
      
    }
    static public void Initialize()
    {
        CreateNavigation();
    }

    static public void LoadNavMesh(string navName)
    {
        LoadTile(navName);
    }
    static public void LoadNavMeshFromData(byte[] data)
    {
        if (data == null ||data.Length == 0)
        {
            Debug.LogError("Navmesh data is null");
            return;
        }
        else
        {
            Debug.Log("Navmesh data load success");
        }
        LoadTileFromBuffer(data, data.Length);
    }

    static public uint GetRandomPointAroundCircle(NavPoint navPoint,float radius,NavPoint navPointOut)
    {
       return FindRandomPointAroundCircle(ref navPoint, radius, ref navPointOut);
    }

    static public uint GetNearestPoly(float[] pos,NavPoint navPoint, float[] polyPickExt)
    {
        return FindNearestPoly(pos, ref navPoint,  polyPickExt);
    }
    static public uint GetNearestPolyNoPickExt(float[] pos, NavPoint navPoint)
    {
        return FindNearestPolyNoPickExt(pos, ref navPoint);
    }

    static public uint GetPathStraight(float[] startPos, float[] endPos, float[] outList, ref int count)
    {
        return FindPathStraight(startPos, endPos,   outList,ref count);
    }

    static public uint GetRaycast(float[] startPos, float[] endPos,float[] hitPos, float[] tangent)
    {
        return FinRaycast(startPos, endPos, hitPos,  tangent);
    }

    static public void Release()
    {
        ReleaseNavigation();
    }

    static public bool IsDtStatusSucceed(uint status)
    {
        return DtStatusSucceed(status);
    }
}